package guowei.gulicart.controller;

import guowei.gulicart.interceptor.CartInterceptor;
import guowei.gulicart.service.CartService;
import guowei.gulicart.vo.Cart;
import guowei.gulicart.vo.CartItem;
import guowei.gulicart.vo.CartUserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.math.BigDecimal;
import java.util.List;
import java.util.concurrent.ExecutionException;

/**
 * 浏览器有一个cookie; user-key;标识用户身份,一个月后过期;
 * 如果第一次使用jd的购物车功能,都会给一个临时的用户身份;浏览器以后保存,每次访间都会带上这个cookie;
 */
@Controller
public class CartController {

    @Autowired
    CartService cartService;

    @GetMapping("/cart.html")
    public String goCartMl(Model model){

        Cart cart = cartService.getCartItems();
        model.addAttribute("cart",cart);

        return "cartList";
    }

//  查看我的购物车访问的地址。应设置重定向。
    @GetMapping("/success")
    public String successHtml(@RequestParam("skuId") Long skuId,
                              @RequestParam("num") Integer num,
                              RedirectAttributes redirectAttributes) throws ExecutionException, InterruptedException {

        CartItem item = cartService.addToCart(skuId,num);

        redirectAttributes.addAttribute("skuId",skuId);
        return "redirect://cart.ganjiuwanle.com/getCartItem.html";
    }

//  查看我的购物车实际访问的地址，有效避免了刷新购物车数量改变的问题
    @GetMapping("/getCartItem.html")
    public String getCartItems(@RequestParam("skuId") Long skuId,Model model){
        CartItem item = cartService.getCartItemByItemId(skuId);
        model.addAttribute("item",item);
        return "success";
    }

//  根据check的值，改变购物车里商品选择状态
    @GetMapping("/checkItem")
    public String ChangeChecked(@RequestParam("skuId") Long skuId,
                                @RequestParam("check") Integer check){

        cartService.changeChecked(skuId,check);
        return "redirect://cart.ganjiuwanle.com/cart.html";
    }

//    根据前端传来的num，改变商品数量
    @GetMapping("/numChange")
    public String numChange(@RequestParam("skuId") Long skuId,
                            @RequestParam("num") Integer num){
        cartService.numChangee(skuId,num);
        return "redirect://cart.ganjiuwanle.com/cart.html";
    }

    @RequestMapping("/deleteItem")
    public String deleteItem(@RequestParam("skuId") Long skuId){

        cartService.deleteRedis(skuId);
        return "redirect://cart.ganjiuwanle.com/cart.html";
    }

    /**
     *   order远程服务得到cartItems
     * @param userkey
     * @return
     */
    @ResponseBody
    @GetMapping("/cart/getOrderItems/{userkey}")
    public List<CartItem> getOrderList(@PathVariable("userkey") String userkey){
        List<CartItem> cartItems = cartService.getOrderItems(userkey);
        return cartItems;
    }

    @ResponseBody
    @GetMapping("/cart/getOrderItems")
    public List<CartItem> getOrderListNoKey(){
        List<CartItem> cartItems = cartService.getOrderItemsNoKeys();
        return cartItems;
    }
}
